package com.boot.springboot.controller;

import com.boot.springboot.entity.Emp;
import com.boot.springboot.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpService empService;
    //获取列表
    @GetMapping("/findAll")
    public String findAll(Model model){
        List<Emp> emps = empService.findAll();
        model.addAttribute("emps",emps);
        return "ems/emplist";
    }


    //添加员工
    @PostMapping("/save")
    public String save(Emp emp){
        boolean is = empService.addEmp(emp);
        if (is==true){
            return "redirect:/emp/findAll";
        }else {
            return "ems/addEmp";
        }
    }

    //删除员工  redirect重定向
    @GetMapping("/delete")
    public String delete(String id){
        empService.deleteEmpById(id);
        return "redirect:/emp/findAll";
    }
    //获取员工信息通过ID  跳转
    @GetMapping("/find")
    public String find(String id,Model model){
        Emp emp = empService.getEmpById(id);
        model.addAttribute("emp",emp);
        return "ems/updateEmp";
    }

    //更行员工信息
    @PostMapping("/update")
    public String update(Emp emp){
        empService.updateEmpById(emp);
        return "redirect:/emp/findAll";
    }
}
